import {Form} from 'antd';
import React, {useRef, useState} from 'react';
import {ProColumns} from '@ant-design/pro-table';
import {Source} from './data.d';
import {modulesUrl} from "@/assets/constant/modulesUrl";
import TableList from "@/assets/components/tableList";
import {hooksSet} from "@/assets/utils/utils";
import {useRequest} from "@@/plugin-request/request";
import {map} from "@/pages/sys/DictList/service";
import {dictType} from "@/assets/constant/dictType";

const SourceList: React.FC<{}> = () => {

  const [form] = Form.useForm();

  /**
   * 获取字典列表
   * @type 字典类型
   */
  const typeDictMap = useRef([]);
  useRequest(() => map({type: dictType.SOURCE_TYPE}), {
    onSuccess: (result, params) => {
      typeDictMap.current = result;
    }
  })
  const databaseTypeDictMap = useRef([]);
  useRequest(() => map({type: dictType.SOURCE_DATABASE_TYPE}), {
    onSuccess: (result, params) => {
      databaseTypeDictMap.current = result;
    }
  })


  const [columns, setColumns] = useState<ProColumns<Source>[]>([
    {
      title: '名称',
      dataIndex: 'name',
    },
    {
      title: '类型',
      dataIndex: 'type',
      renderText: (text) => typeDictMap.current[text]
    },
    {
      title: '数据库',
      dataIndex: 'databaseType',
      hideInSearch: true,
      renderText: (text) => databaseTypeDictMap.current[text]
    },
    {
      title: '数据库连接',
      dataIndex: 'databaseUrl',
      hideInSearch: true,
    },
  ]);


  return (
    <TableList
      form={form}
      changeColumns={(columns: any) => hooksSet(setColumns, columns)}
      columns={columns}
      moduleUrl={modulesUrl.reportSource}
      title={"数据源管理"}
      isNotUseModalForm={true}
    />
  );
}

export default SourceList;
